﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _3.Fractions
{
    public class Fraction
    {
        public int Numerator { get; set; }
        public int Denominator { get; set; }

        public Fraction(int numerator, int denominator)
        {
            this.Numerator = numerator;
            this.Denominator = denominator;
        }

        public Fraction()
            :this(0, 0)
        {
        }

        public override string ToString()
        {
            if (this.Denominator == 0)
            {
                return "undefined";                        
            }

            if (this.Numerator == 0)
            {
                return "0";
            }

            return String.Format("{0}/{1}", this.Numerator, this.Denominator);
        }

        public string ToString(string outputFormat)
        {
            if (outputFormat == "N")
            {
                return this.ToString();
            }
            else if (outputFormat == "R")
            {
                if (this.Denominator == 0)
                {
                    throw new DivideByZeroException("Denominator");
                }
                else
                {
                    double value = (double)this.Numerator / this.Denominator;
                    return value.ToString(); 
                }
            }
            else
            {
                throw new FormatException("Not a recognized format.");
            }
        }
    }
}
